<?php
require_once('../carbox.php');
if(!carbox_check_session( ))
	header('Location: http://'.$_SERVER['HTTP_HOST'].'/carbox/login.php');
$views = end(carbox_query(
	"SELECT DISTINCT d.process_id, action, DATE_FORMAT(da.date, '%d/%m/%Y') AS fdate FROM Document d INNER JOIN DocumentAction AS da ON d.id = da.document_id INNER JOIN Action AS a ON da.action_id = a.id WHERE da.user_id = :uid ORDER BY da.date DESC LIMIT 50",
	array(':uid' => $_SESSION['uid'])
));
switch($_SESSION['profile']) {
	case 2:
		$sql = "
			SELECT DISTINCT p.processo_id, p.numero
			FROM Processo p
			INNER JOIN processo_cliente AS pc ON p.processo_id = pc.processo_id
			INNER JOIN usuario_empreendedor AS ue ON pc.cliente_id = ue.cliente_id
			WHERE pc.cliente_id <> '0000000196' AND ue.usuario_empreendedor_id = :uid
			ORDER BY p.numero DESC
		";
		break;
	case 3:
	case 4:
	case 5:	
		$sql = "SELECT DISTINCT processo_id, numero FROM Processo WHERE responsavel_id = :uid ORDER BY numero DESC";
		break;
}
$procs = end(cerberus_query($sql, array(':uid' => carbox_get_cerberus_uid($_SESSION['uid']))));
/*
$query = end(cerberus_query($sql, array(':uid' => carbox_get_cerberus_uid($_SESSION['uid']))));
$procs = NULL;
for($i = 0; $i < count($query); ++$i) {
	$sql = "
		SELECT DISTINCT process_id
		FROM Document d
		WHERE d.process_id = :pid
	";
	$params = array(':pid' => $query[$i]['processo_id'], ':uid' => $_SESSION['uid'], ':profile' => $_SESSION['profile']);
	$found = carbox_query($sql, $params);
	if(!$found['fail'] && !empty($found['result'])) {
		$procs[ ] = $query[$i]; 
	}
}
*/
?>
<div class="container-fluid">
	<div class="row-fluid">
		<div class="span6">
			<table class="table table-condensed" id="table-last-views">
				<thead>
					<tr>
						<th><input type="checkbox" class="checkbox" id="checkbox-select-all-view-procs" /></th>						
						<th><i class="icon-eye-open"></i> Processos</th>
						<th><i class="icon-tasks"></i> Ação</th>
						<th><i class="icon-calendar"></i> Data</th>
					</tr>
				</thead>
				<tbody>
					<?
					for($i = 0; $i < count($views); ++$i) {
						echo '
							<tr>
								<td><input type="checkbox" class="checkbox checkbox-views" value="'.$views[$i]['process_id'].'" /></td>
								<td>'.carbox_get_pn_by_pid($views[$i]['process_id']).'</td>
								<td>'.$views[$i]['action'].'</td>
								<td>'.$views[$i]['fdate'].'</td>
							</tr>
						';
					}
					?>
				</tbody>
			</table>
		</div>
		<div class="span6">
			<table class="table table-condensed" id="table-processes">
				<thead>
					<tr>
						<th><input type="checkbox" class="checkbox" id="checkbox-select-all-my-procs" /></th>						
						<th><i class="icon-list-alt"></i> Meus Processos</th>
					</tr>
				</thead>
				<tbody>
					<?
					for($i = 0; $i < count($procs); ++$i)
						echo '
							<tr>
								<td><input type="checkbox" class="checkbox checkbox-procs" value="'.$procs[$i]['processo_id'].'" /></td>
								<td>'.$procs[$i]['numero'].'</td>
							</tr>
						';
					?>
				</tbody>
			</table>
		</div>
	</div>
	<div class="row-fluid">
		<div class="pagination pagination-centered pagination-small">
			<ul>
				<?php
				$buttons = array(
					'<li><a data-toggle="modal" href="php/account/ui_change_password.php" data-target="#modal-change-password" role="button" id="btn-password"><i class="icon-cog"></i> Alterar Senha</a></li>',
					'<li><a href="#" id="btn-user"><i class="icon-user"></i> Gerenciar Contas</a></li>',
					'<li><a href="#" id="btn-summarize"><i class="icon-th-list"></i> Indexar Documento</a></li>',
					'<li><a href="#" id="btn-view"><i class="icon-eye-close"></i> Visualizar Documentos</li>',
					'<li><a href="#" id="btn-charts"><i class="icon-picture"></i> Ver Relatórios</a></li>',
					'<li><a href="CARBOX.pdf" target="_blank"><i class="icon-download"></i> Baixar Manual</a></li>'
				);
				switch($_SESSION['profile']) {
					case 2:
					case 3:
						echo $buttons[3];
						echo $buttons[0];
						echo $buttons[5];
						break;
					case 4:
						echo $buttons[2];
						echo $buttons[3];
						echo $buttons[0];
						echo $buttons[5];
						break;
					case 5:
						echo $buttons[1];
						echo $buttons[2];
						echo $buttons[3];
						echo $buttons[4];
						echo $buttons[0];
						echo $buttons[5];
						break;
				}
				?>	
			</ul>
		</div>	
	</div>
</div>
<div class="modal hide fade" id="modal-change-password" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3 id="myModalLabel">Alterar Senha</h3>
	</div>
	<div class="modal-body">
	</div>
	<div class="modal-footer">
		<button type="button" class="btn" id="btn-confirm-change-password">Confirmar</button>
	</div>
</div>

<div class="modal hide fade" id="modal-summarize" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3 id="myModalLabel">Indexar Documento</h3>
	</div>
	<div class="modal-body">
		
	</div>
	<div class="modal-footer">
		<button type="button" class="btn" id="btn-confirm-summarize">Confirmar</button>
	</div>
</div>
<script type="text/javascript"> $(carbox_ui_account( )); </script>
